﻿
namespace Demo.Microservice.Registrys
{
    /// <summary>
    /// 启动项目，实现服务注册
    /// </summary>
    public class ServiceRegistryHostService : IHostedService
    {
        public ConsulServiceRegistry consulServiceRegistry { get; set; }

        public ServiceRegistryHostService(ConsulServiceRegistry consulServiceRegistry)
        {
            this.consulServiceRegistry = consulServiceRegistry;
        }

        /// <summary>
        /// 项目启动，调用服务注册方法
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StartAsync(CancellationToken cancellationToken)
        {
            consulServiceRegistry.Registry();
            return Task.CompletedTask;
        }

        /// <summary>
        /// 项目关闭，调用服务删除方法
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StopAsync(CancellationToken cancellationToken)
        {
            consulServiceRegistry.DeRegistry();
            return Task.CompletedTask;
        }
    }
}
